package com.example.tests;

import static com.example.tests.ContactDataGenerator.loadContactsFromXmlFile;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.example.utils.SortedListOf;

public class ContactCreationTests extends TestBase {
	
	@DataProvider
	public Iterator<Object[]> contactsFromFile() throws IOException {
		//return wrapContactDataForProvider(loadContactsFromCsvFile(new File("contacts.txt"))).iterator();
		return wrapContactDataForProvider(loadContactsFromXmlFile(new File("contacts.xml"))).iterator();
	}
  
	//@Test(dataProvider = "randomValidContactsGenerator")
	@Test(dataProvider = "contactsFromFile")
	public void testContactCreationWithValidData(ContactData contact) throws Exception {
	    
	  SortedListOf<ContactData> oldList = app.getModel().getContacts();    
	  app.getContactHelper().createContact(contact);    
	  SortedListOf<ContactData> newList = app.getModel().getContacts();
	     
	  assertThat(newList, equalTo(oldList.withAdded(contact)));
	    
	  if (wantToCheck()) {
		  if ("yes".equals(app.getProperty("check.db"))) {	  
			  ContactData.assertDbContactsEqual(app.getModel().getContacts(), app.getHibernateHelper().listContacts());
			  System.out.println("DB verification passed");
		  }
		  if ("yes".equals(app.getProperty("check.ui"))) {

			  SortedListOf<ContactDataRow> mdContacts = ContactDataRow.buildFromContactList(app.getModel().getContacts()); 	  
			  SortedListOf<ContactDataRow> uiContacts = app.getContactHelper().getUIContacts();
			  
			  assertThat("UI verification failed", mdContacts, equalTo(uiContacts));
			  System.out.println("UI verification passed");
			   
		  }
		  
	  }
	  
	}
   
}

